package com.xingkeduo.business.user.service;

import com.xingkeduo.business.user.dao.CraftsmanServiceMapper;
import com.xingkeduo.dao.database.DatabaseAccessObject;
import com.xingkeduo.pojo.query.CraftsmanServiceQO;
import com.xingkeduo.pojo.table.CraftsmanServiceTO;
import com.xingkeduo.util.pojo.exception.DataException;
import com.xingkeduo.util.tool.CommonUtil;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Iterator;
import java.util.List;

/**
 * @Description:
 * @Author:gaozifeng
 * @Date 2019/5/6 14:24
 **/
@Service
public class CraftsmanServiceService {

    private static final String NAMESPACE = CommonUtil.formatNamespace(CraftsmanServiceTO.class);
    @Resource(name = "mysqlUserMasterDao")
    private DatabaseAccessObject<CraftsmanServiceTO, CraftsmanServiceQO> dao;

    @Resource
    private CraftsmanServiceMapper craftsmanServiceMapper;

    /**
     * 查询
     *
     * @param code
     * @return
     * @throws Throwable
     * @author 高杰
     * @time 2018年12月26日 下午6:26:11
     */
    public CraftsmanServiceTO selectByPrimary(long id) throws Throwable {
        return dao.select(NAMESPACE, id);
    }

    /**
     * 查询
     *
     * @param id
     * @return
     * @throws Throwable
     * @author 高杰
     * @time 2019年1月8日 下午3:49:16
     */
    public CraftsmanServiceTO selectByPrimaryWithHistory(long id) throws Throwable {
        CraftsmanServiceTO craftsmanTable = dao.select(NAMESPACE, id);
        return craftsmanTable;
    }


    /**
     * 查询主键
     *
     * @param query
     * @return
     * @throws Throwable
     * @author 高杰
     * @time 2019年1月8日 下午2:13:33
     */
    public List<Long> selectPrimatyByQuery(CraftsmanServiceQO query) throws Throwable {
        return dao.selectIndex(NAMESPACE, query);
    }

    /**
     * 查询
     *
     * @param query
     * @return
     * @throws Throwable
     * @author 高杰
     * @time 2019年1月8日 下午2:22:21
     */
    public List<CraftsmanServiceTO> selectByQuery(CraftsmanServiceQO query) throws Throwable {
        return dao.select(NAMESPACE, query);
    }

    /**
     * 查询总数
     *
     * @param query
     * @return
     * @throws Throwable
     * @author 高杰
     * @time 2019年1月9日 下午5:06:44
     */
    public int count(CraftsmanServiceQO query) throws Throwable {
        return dao.selectCount(NAMESPACE, query);
    }

    /**
     * 添加
     *
     * @param craftsman
     * @return
     * @throws Throwable
     * @author 高杰
     * @time 2018年12月27日 上午11:13:21
     */
    @Transactional
    public void insert(CraftsmanServiceTO craftsman) throws Throwable {
        if (dao.insert(NAMESPACE, craftsman) < 1)
            throw new DataException(DataException.Type.FAIL, "一线员工账户信息");
    }

    /**
     * 添加
     *
     * @param craftsman
     * @return
     * @throws Throwable
     * @author 高杰
     * @time 2018年12月27日 上午11:13:21
     */
    @Transactional
    public void insertList(List<CraftsmanServiceTO> list) throws Throwable {
        if (dao.insert(NAMESPACE, list) < list.size())
            throw new DataException(DataException.Type.FAIL, "一线员工账户信息");
    }

    /**
     * 修改
     *
     * @param craftsman
     * @return
     * @throws Throwable
     * @author 高杰
     * @time 2018年12月27日 上午11:37:28
     */
    @Transactional
    public void update(CraftsmanServiceTO craftsman) throws Throwable {
        if (dao.update(NAMESPACE, craftsman) < 1)
            throw new DataException(DataException.Type.FAIL, "一线员工账户信息");
    }

    /**
     * 批量修改
     *
     * @param list:
     * @return : int
     * @author : gaozifeng
     * @date : 2019/5/6 14:57
     */
    @Transactional
    public int updateList(List<CraftsmanServiceTO> list) throws Throwable {
        int count = 0;
        for (Iterator<CraftsmanServiceTO> iterator = list.iterator(); iterator.hasNext(); ) {
            count += dao.update(NAMESPACE, iterator.next());
        }
        return count;
    }


    public int selectCount(CraftsmanServiceQO serviceQO) {
        int retVal = craftsmanServiceMapper.countGroupBy(serviceQO);
        return retVal;
    }

}
